import 'package:flutter_menu/data/obx_store.dart';
import 'package:objectbox/objectbox.dart';
import 'package:json_annotation/json_annotation.dart';

import '../objectbox.g.dart';

part 'login_model.g.dart';

@JsonSerializable()
class LoginReq {
  ///唯一id 由pos上传
  String deviceId;

  ///设备名称
  String deviceName;

  ///设备sn
  String devicesSn;

  ///内网ip
  String electronicMenuIntranetIp;

  ///电子菜单版本号
  String electronicMenuVersionName;

  ///设备型号
  String model;

  ///店铺id
  String shopId;

  LoginReq(
      this.deviceId,
      this.deviceName,
      this.devicesSn,
      this.electronicMenuIntranetIp,
      this.electronicMenuVersionName,
      this.model,
      this.shopId);

  factory LoginReq.fromJson(Map<String, dynamic> json) =>
      _$LoginReqFromJson(json);

  Map<String, dynamic> toJson() => _$LoginReqToJson(this);
}

@JsonSerializable()
@Entity()
class UserModel with IEntity{
  UserModel({this.obxId = 0,this.updateTime = 0});

  @Id()
  int obxId;

  ///允许登录移动设备 0否 1是
  int? allowLoginDevice;

  ///小费抽成比例
  int? distributionRate;

  @override
  int updateTime ;

  ///uuid
  @Index()
  String? id;

  ///用户昵称
  String? nickName;

  ///用户密码
  String? password;

  ///用户名
  String? username;

  ///用户编号
  String? userNumber;



  bool isAllowLoginDevice() => allowLoginDevice==null;

  factory UserModel.fromJson(Map<String, dynamic> json) => _$UserModelFromJson(json);
  Map<String, dynamic> toJson() => _$UserModelToJson(this);

  static ToOne<UserModel> toOneFromJson(Map<String, dynamic> json) =>
      ToOne(target: UserModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<UserModel> s) =>
      s.target != null ? _$UserModelToJson(s.target!) : {};

  static ToMany<UserModel> toManyFromJson(List<dynamic>? json) =>
      ToMany(items: json?.map((e) => UserModel.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<UserModel>? s) =>
      s?.toList().map((e) => e.toJson()).toList();

  @override
  int update(Store store, int? time) {
    updateTime = time ?? updateTime;
    var box = store.box<UserModel>();
    return box.update(this, UserModel_.id, id!);
  }
}
